package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.ActorMapper;
import com.zretc.dao.FilmRankMapper;
import com.zretc.dao.FilmTypeMapper;
import com.zretc.entity.Actor;
import com.zretc.entity.Film;
import com.zretc.entity.FilmRank;
import com.zretc.entity.FilmType;
import com.zretc.service.FilmRankService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FilmRankServiceImpl implements FilmRankService {

    private static final Logger logger = LoggerFactory.getLogger(FilmRankServiceImpl.class);

    @Autowired
    private FilmRankMapper filmRankMapper;

    @Autowired
    private ActorMapper actorMapper;

    @Autowired
    private FilmTypeMapper filmTypeMapper;

   /* @Override
    public boolean insertFilmRank(FilmRank filmRank) {

        return false;
    }*/

    @Override
    public boolean deleteFilmRankByFilmId(Integer filmId) {
        int i = filmRankMapper.deleteFilmRankByFilmId(filmId);
        if (i > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    @Transactional
    public boolean updateFilmRankByFilmRankId(FilmRank filmRank) {

        // 传入的参数影片编号
        Integer filmId = filmRank.getFilm().getFilmId();

        // 根据传入的参数影片编号，查询到当前的排行名次
        FilmRank filmRankByFilmId = filmRankMapper.findFilmRankByFilmId(filmId);
        Integer oldNumber = filmRankByFilmId.getRankNumber();
        logger.info("======================================旧的排名：" + oldNumber);

        // 传入的排行名次
        Integer rankNumber = filmRank.getRankNumber();

        /*// 根据传入的排行名次，查询到要被修改影片对象的编号
        FilmRank filmRankByRankNumber = filmRankMapper.findFilmRankByRankNumber(rankNumber);
        Integer oldFilmId1 = filmRankByRankNumber.getFilm().getFilmId();
        logger.info("======================================被修改的排名对象：" + oldNumber);*/

        FilmRank newfilmRank = null;
        Film film = null;

        // 从查询的的影片对象开始排行名次都+1，直到被替换的影片对象排行名次为止
        Integer count = oldNumber - rankNumber;
        logger.info("======================================要修改的排名次数：" + count);
        if (count > 0) {
            // 排行上升，被修改的对象和之后的对象排行+1
            logger.info("======================================执行上升排序");
            for (int i = 1; i <= oldNumber - rankNumber; i++) {
                Integer shangNumber = oldNumber - i;
                logger.info("===========================================================上一级的排行：" + shangNumber);
                FilmRank filmRankByshang = filmRankMapper.findFilmRankByRankNumber(shangNumber);
                logger.info("===========================================================上一级：" + filmRankByshang);
                Integer oldFilmId = filmRankByshang.getFilm().getFilmId();
                newfilmRank = new FilmRank();
                film = new Film();
                film.setFilmId(oldFilmId);
                newfilmRank.setFilm(film);
                newfilmRank.setRankNumber(shangNumber + 1);
                int shang = filmRankMapper.updateFilmRankByFilmRankId(newfilmRank);
                if (shang > 0) {
                    logger.info("=================================上升排行顺序修改成功==========================");
                } else {
                    logger.info("======================================上升排行顺序修改失败==========================");
                }
            }
        }
        if (count < 0) {
            // 排行下降，被修改的对象和之后的对象排行-1
            logger.info("======================================执行下降排序");
            for (int i = oldNumber; i < rankNumber; i++) {
                Integer xiaNumber = i + 1;
                logger.info("===========================================================下一级的排行：" + xiaNumber);
                FilmRank filmRankByXia = filmRankMapper.findFilmRankByRankNumber(xiaNumber);
                logger.info("===========================================================下一级：" + filmRankByXia);
                Integer oldFilmId = filmRankByXia.getFilm().getFilmId();
                newfilmRank = new FilmRank();
                film = new Film();
                film.setFilmId(oldFilmId);
                newfilmRank.setFilm(film);
                newfilmRank.setRankNumber(i);
                int xia = filmRankMapper.updateFilmRankByFilmRankId(newfilmRank);
                if (xia > 0) {
                    logger.info("=================================下降排行顺序修改成功==========================");
                } else {
                    logger.info("======================================下降排行顺序修改失败==========================");
                }
            }

        }
        // 开始修改排行名次
        newfilmRank = new FilmRank();
        film = new Film();
        film.setFilmId(filmId);
        newfilmRank.setFilm(film);
        newfilmRank.setRankNumber(rankNumber);

        int n = filmRankMapper.updateFilmRankByFilmRankId(newfilmRank);
        if (n > 0) {
            logger.info("=================================修改新排行成功==========================");
            return true;
        } else {
            logger.info("======================================修改新排行失败==========================");
            return false;
        }
    }

    @Override
    public PageInfo<FilmRank> getFilmRankByCondition(Map<String, String> params) {
        // 分页页码
        String number = params.get("pageNum");
        if ("".equals(number)) {
            number = "1";
        }
        Integer pageNum = Integer.valueOf(number);

        String size= params.get("pageSize");
        if ("".equals(size)) {
            number = "10";
        }
        Integer pageSize = Integer.valueOf(size);
        // 影片名称
        String filmName = params.get("filmName");
        // 上映时间
        String filmReleaseTime = params.get("filmReleaseTime");

        Map<String, String> map = new HashMap<>();
        if ("".equals(filmName) || filmName == null) {
            map.put("filmName",null);
        } else {
            map.put("filmName","%" + filmName + "%");
        }
        if ("".equals(filmReleaseTime) || filmReleaseTime == null) {
            map.put("filmReleaseTime",null);
        } else {
            map.put("filmReleaseTime","%" + filmReleaseTime + "%");
        }

        // 开始分页
        PageHelper.startPage(pageNum,pageSize);
        // 进行分页
        List<FilmRank> filmRankList = filmRankMapper.findFilmRankListByCondition(map);
        //用PageInfo对结果进行包装
        PageInfo<FilmRank> pageInfoFilmRank = new PageInfo<>(filmRankList);

        return pageInfoFilmRank;
    }

    @Override
    public List<FilmRank> getFilmRankListByLimit() {

        List<FilmRank> filmRankListByLimit = filmRankMapper.findFilmRankListByLimit();

        return filmRankListByLimit;
    }
}
